
var phrases = [
{
"Phrase":"Boat",
"Category":"Misc",
"Difficulty":"Easy"
}
,
{
"Phrase":"Couch",
"Category":"Misc",
"Difficulty":"Easy"
}
,
{
"Phrase":"Chair",
"Category":"Misc",
"Difficulty":"Easy"
}
,
{
"Phrase":"Bat",
"Category":"Sports",
"Difficulty":"Easy"
}
,
{
 "Phrase":"Goal",
 "Category":"Sports",
 "Difficulty":"Easy"
 }
 ,
 {
  "Phrase":"Golf Club",
  "Category":"Sports",
  "Difficulty":"Easy"
  }
  ,
  {
   "Phrase":"Cheese",
   "Category":"Food",
   "Difficulty":"Easy"
   }
   ,
   {
    "Phrase":"Potato",
    "Category":"Food",
    "Difficulty":"Easy"
    }
    ,
    {
     "Phrase":"Beans",
     "Category":"Food",
     "Difficulty":"Easy"
     }
     ,
     {
      "Phrase":"Bread",
      "Category":"Food",
      "Difficulty":"Easy"
      }
]
;



var categoryQueue = [];
var phraseQueue = [];
var usedPhraseList = [];


var category = "";
var phrase = "";
var seconds = 0;
var minutes = 0;
var clock = 0;
var maxscore =10;
var difficulty="Normal"
var team1=0;
var team2=0;
var mode = "classic"; // classic = first to x points. time attack is most points at x time



function init()
{

loadQueues();
loadList();
}

setInterval(function() {
	timer();
}, 1000);

//setInterval(function() {updateUI();}, 250);


function loadQueues()
{
	objx(phrases).each(function(item) {
	if(categoryQueue.length<1)
	{
	categoryQueue.push(item.Category);
	}
	else
	{
	if(categoryQueue.indexOf(item.Category)==-1)
	{
	categoryQueue.push(item.Category);
	}
	}
		var tempObj= {};
		tempObj.Category=item.Category;
		tempObj.Phrase=item.Phrase;
		tempObj.Difficulty=item.Difficulty;
		phraseQueue.push(tempObj);
	});
}


function loadList()
{
for (i=0;i<categoryQueue.length;i++)
{
var x = document.getElementById("categorylist");
		var option = document.createElement("option");
		option.text = categoryQueue[i];
		x.add(option);
		category=x[0].text;
}
}

function checkwinner()
{
var winner= '';
if(mode=="classic")
{
if(team1== maxscore || team2==maxscore)
{

if(team1>team2)
{
alert("Team 1 Wins!");
clearPoints();
}
else
{
alert("Team 2 Wins!")
clearPoints();
}

}

}
}


function getPhrase()
{
var tempArray=[];
for(i=0;i<phraseQueue.length;i++)
{

if(phraseQueue[i].Category==category && phraseQueue[i].Difficulty==difficulty)
{
if(usedPhraseList.indexOf(phraseQueue[i].Phrase)==-1)
{
tempArray.push(phraseQueue[i].Phrase);
}
}
}

if(tempArray.length<1)
{
usedPhraseList.length=0;
for(i=0;i<phraseQueue.length;i++)
{

if(phraseQueue[i].Category==category && phraseQueue[i].Difficulty==difficulty)
{
tempArray.push(phraseQueue[i].Phrase);
}
}
}

var randPhrase = tempArray[randomIntFromInterval(0,tempArray.length-1)];
if(randPhrase!= undefined)
{
document.getElementById("phrase").value= randPhrase;
usedPhraseList.push(randPhrase);
}
else
{
alert("No Phrases with that difficulty");
}
}

function maxscorechange()
{
maxscore = document.getElementById("maxscore").value;
}

function changedifficulty()
{
difficulty = document.getElementById("difficulty").value;
}

function setCategory()
{
category = document.getElementById("categorylist").value;
}

function updateClock()
{
document.getElementById("timer").value= minutes+":"+seconds;
}

function timer()
{
if(clock==1)
{
seconds = seconds +1;
if(seconds> 60)
{
minutes = minutes +1;
seconds =0;
}
updateClock();
}
//code here runs every second

}



function updateTeam1()
{

team1= team1+1;
document.getElementById("team1").value= team1;
checkwinner();
}

function updateTeam2()
{
team2=team2+1;
document.getElementById("team2").value= team2;
checkwinner();
}


function updateUI()
{
document.getElementById("timer").value= minutes+":"+seconds;
document.getElementById("team1").value= team1;
document.getElementById("team2").value= team2;
}

function updateClock()
{
document.getElementById("timer").value= minutes+":"+seconds;
}

function toggleTimer()
{
if(clock==1)
{
seconds= 0;
minutes=0;
clock=0;
document.getElementById("phrase").value="";
}
else
{
clock=1;
getPhrase();
}
}


function clearPoints()
{
team1=0;
team2=0;
toggleTimer();
updateUI();

}

function randomIntFromInterval(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

